# -!- coding: utf-8 -!-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#作者：cacho_37967865
#博客：https://blog.csdn.net/sinat_37967865
#文件：get_stock_organization.py
#日期：2021-01-22
#备注：东方财富通统计机构持仓数量
http://emweb.securities.eastmoney.com/ShareholderResearch/MainPositionsHodlerAjax?date=2020-12-31&code=SZ300285
CREATE TABLE `stock_organization` (
  `stock_no` varchar(20) NOT NULL,
  `stock_name` varchar(20) NOT NULL,
  `holder_date` varchar(20) NOT NULL COMMENT '统计周期', 
  `holder_type` varchar(20) NOT NULL COMMENT '机构类型',  
  `holder_num` int(4) NOT NULL DEFAULT 0 COMMENT '机构数量',
  `stock_num` bigint(20) NOT NULL DEFAULT 0 COMMENT '持仓股数(股)', 
  `flow_rate` float NOT NULL DEFAULT 0.0 COMMENT '占流通股比例',  
  `total_rate` float NOT NULL DEFAULT 0.0 COMMENT '占总股本比例',   
  `create_time` datetime NOT NULL COMMENT '创建时间',
  PRIMARY KEY (`stock_no`,`holder_date`,`holder_type`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='沪深股票机构持仓数量';
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
import requests
import time
import threading
from pacong.stock.stock_config import db,cursor,headers
from pacong.stock.stock_base import get_stock_nos
from pycacho.cachobase.logger import Logger

logger = Logger("get_stock_organization").get_log()
lock = threading.Lock()

holder_date =['2020-12-31','2020-09-30','2020-06-30','2020-03-31','2019-12-31']

def stock_organization(holder_date,code,stock_name):
    url = 'http://emweb.securities.eastmoney.com/ShareholderResearch/MainPositionsHodlerAjax?date={0}&code={1}'.format(holder_date,code)
    resp = requests.get(url, headers=headers)
    data = resp.json()
    for info in data:
        stock_no = code.replace('SZ','').replace('SH','')
        holder_type = info['jglx']
        holder_num = info['ccjs'].replace('--','0')
        stock_num = info['ccgs'].replace('--','0')
        flow_rate = info['zltgbl'].replace('--','0').replace('%','')
        total_rate = info['zltgbbl'].replace('--','0').replace('%','')
        print(stock_no,stock_name,holder_date,holder_type,holder_num,stock_num,flow_rate,total_rate)
        with lock:
            organization_to_mysql(stock_no, stock_name, holder_date, holder_type, holder_num, stock_num, flow_rate, total_rate)


def organization_to_mysql(stock_no,stock_name,holder_date,holder_type,holder_num,stock_num,flow_rate,total_rate):
    sql = "insert into stock_organization (stock_no,stock_name,holder_date,holder_type,holder_num,stock_num,flow_rate,total_rate,create_time) " \
          "values ('%s','%s','%s','%s','%s','%s','%s','%s',now());" \
          % (stock_no,stock_name,holder_date,holder_type,holder_num,stock_num,flow_rate,total_rate)
    try:
        cursor.execute(sql)
    except Exception as e:
        # 发生错误时回滚
        db.rollback()
        print(str(e))
    else:
        db.commit()
        #logger.info('事务处理成功')

def main():
    stocks = get_stock_nos()
    for stock in stocks:
        for date in holder_date:
            stock_organization(date,stock[0],stock[1])


def batch():
    stocks = get_stock_nos()
    for stock in stocks:
        threads = [threading.Thread(target=stock_organization, args=(date,stock[0],stock[1])) for date in holder_date]
        for t in threads:
            t.start()

        for t in threads:
            t.join()

if __name__ == '__main__':
    cTime = time.time()
    stock_organization('2018-12-31','SZ300285','国瓷材料')
    #batch()
    logger.info("总共耗时：%.2f s ", time.time() - cTime)